{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 登录注册"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、创建表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导包\n",
    "import pymysql\n",
    "\n",
    "# 创建连接\n",
    "con = pymysql.connect(\n",
    "    host=\"master\",\n",
    "    port=3306,\n",
    "    user=\"root\",\n",
    "    password=\"123456\",\n",
    "    database=\"shujia\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取游标，执行sql\n",
    "cursor = con.cursor()\n",
    "\n",
    "# 创建保存用户信息表\n",
    "user_ddl = \"\"\"\n",
    "create table users(\n",
    "  `id` INT UNSIGNED AUTO_INCREMENT,\n",
    "  `name` varchar(255) not null,\n",
    "  `phone` varchar(11) not null,\n",
    "  `addr` varchar(255) ,\n",
    "  `email` varchar(255) ,\n",
    "  `passwd` varchar(255) not null ,\n",
    "   PRIMARY KEY ( `id` )\n",
    ")\n",
    "\"\"\"\n",
    "\n",
    "# 执行sql\n",
    "cursor.execute(user_ddl)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、注册\n",
    "用户输入用户名密码手机号，点击注册，验证用户名是否存在，手机号格式是否正确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=========================用户注册==============================\n",
      "insert into users(name,phone,passwd) values('q','123','123')\n"
     ]
    }
   ],
   "source": [
    "print(\"=========================用户注册==============================\")\n",
    "# 用户名\n",
    "username = input(\"请输出用户名：\")\n",
    "\n",
    "# 手机号\n",
    "phone = input(\"请输入手机号：\")\n",
    "\n",
    "# 密码\n",
    "passwd = input(\"请输入密码：\")\n",
    "\n",
    "# 拼接sql\n",
    "insert_sql = f\"insert into users(name,phone,passwd) values('{username}','{phone}','{passwd}')\"\n",
    "\n",
    "# 执行sql\n",
    "flog = cursor.execute(insert_sql)\n",
    "if flog !=0:\n",
    "    print(\"注册成功\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2c4166c4e60205886b39594020a0a92964d0c5302c672db710129f3f2bb29b9f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
